﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Common;
using Persistence.Dao;
using Domain;
using Persistence;
namespace web.manage
{
    public partial class flashad_operate : adminBase
    {
        int id = DNTRequest.GetQueryInt("id", 0);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (id > 0)
                {
                    FlashAD entity_FlashAD = Singleton<FlashADDao>.Instance.GetModel(id);
                    if (entity_FlashAD != null)
                    {
                        ViewState["entity_FlashAD"] = entity_FlashAD;
                        txt_FlashAD_description.Value = entity_FlashAD.description;
                        txt_FlashAD_linkUrl.Value = entity_FlashAD.linkUrl;
                        txt_FlashAD_adOrder.Value = entity_FlashAD.adOrder.ToString();
                        if (string.IsNullOrEmpty(entity_FlashAD.imagePath))
                        {
                            hlImage.Visible = false;
                        }
                        else
                        {
                            hlImage.NavigateUrl = entity_FlashAD.imagePath;
                        }
                    }
                }
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            FlashAD entity_FlashAD = new FlashAD();
            if (id > 0 && ViewState["entity_FlashAD"] != null)
            {
                entity_FlashAD = (FlashAD)ViewState["entity_FlashAD"];
            }
            entity_FlashAD.description = txt_FlashAD_description.Value.Trim();
            if (fileImage.Value != "")
            {
                UpLoad u = new UpLoad();
                u.Path = "images/ad";
                u.ModifyFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond;
                entity_FlashAD.imagePath = u.SaveAs(fileImage);
            }
            entity_FlashAD.linkUrl = txt_FlashAD_linkUrl.Value.Trim();
            entity_FlashAD.adOrder = Utils.ObjectToInt32(txt_FlashAD_adOrder.Value.Trim(), 0);
            entity_FlashAD.addDate = DateTime.Now;
            bool bResult = false;
            if (id > 0)
            {
                bResult = Singleton<FlashADDao>.Instance.Update(entity_FlashAD);
            }
            else
            {
                try
                {
                    Singleton<FlashADDao>.Instance.Insert(entity_FlashAD);
                    bResult = true;
                }
                catch { }
            }
            if (bResult)
            {
                Message.alert("操作成功");
                txt_FlashAD_description.Value = "";
                txt_FlashAD_linkUrl.Value = "";
                txt_FlashAD_adOrder.Value = "";
            }
            else
            {
                Message.alert("操作失败，请检查您的数据");
            }
        }
    }
}
